package com.example.testmarkwon

import android.content.Context
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.WindowManager
import kotlin.math.roundToInt

val displayMetrics: DisplayMetrics
    get() = App.app.resources.displayMetrics

@Suppress("DEPRECATION")
val realDisplayMetrics: DisplayMetrics
    get() {
        (App.app.getSystemService(Context.WINDOW_SERVICE) as WindowManager).let { wm ->
            val dm = DisplayMetrics()
            wm.defaultDisplay.getRealMetrics(dm)
            return dm
        }
    }

val realScreenHeight: Int get() = realDisplayMetrics.heightPixels

val realScreenWidth: Int get() = realDisplayMetrics.widthPixels

val screenHeight: Int get() = displayMetrics.heightPixels

val screenWidth: Int get() = displayMetrics.widthPixels

val Float.dp: Float get() = this * getDensity()

val Int.dp: Float get() = this * getDensity()

val Float.dpInt: Int get() = dp.roundToInt()

val Int.dpInt: Int get() = dp.roundToInt()

val Float.sp: Float
    get() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_SP,
        this,
        displayMetrics
    )

val Int.sp: Float get() = toFloat().sp

val Float.spInt: Int get() = sp.roundToInt()

val Int.spInt: Int get() = sp.roundToInt()

fun Float.px2Dp(): Float = this / getDensity()

fun Int.px2Dp(): Int = (this / getDensity()).roundToInt()

private fun getDensity(): Float = displayMetrics.density